package com.deparse.dpframe.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;

import com.deparse.dpframe.R;
import com.deparse.dpframe.ui.interfaces.OnDispatchTouchEventListener;
import com.deparse.dpframe.utils.DPLogUtil;

/**
 * Created by hsy on 2016/2/24.
 */
public class DPTabItemContainer extends LinearLayout implements OnDispatchTouchEventListener {

    private View currentTabWidget = null;
    private int currentTabIndex = -1;
    private boolean recyclable = false;
    private OnTabClickListener onTabClickListener;
    private OnTabChangeListener externalTabChangeListener = null;
    private OnTabChangeListener tabChangeListener = new OnTabChangeListener() {
        @Override
        public void beforeTabChange(int index, View tabWidget) {
            if (null != externalTabChangeListener) {
                externalTabChangeListener.beforeTabChange(index, tabWidget);
            }
        }

        @Override
        public void afterTabChange(int index, View tabWidget) {
            if (null != externalTabChangeListener) {
                externalTabChangeListener.afterTabChange(index, tabWidget);
            }
        }
    };

    public DPTabItemContainer(Context context) {
        super(context);
        init(context);
    }

    public DPTabItemContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DPTabItemContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        DPLogUtil.d("DPTabItemContainer.init");
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        DPLogUtil.d("DPTabItemContainer.onAttachedToWindow childCount=" + getChildCount());
        addChildrenEvents();
    }

    private void addChildrenEvents() {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof DPTabItem) {
                child.setTag(R.id.dp_TABHOST_TAG_TAB_INDEX, i);
                ((DPTabItem) child).setOnDispatchTouchEventListener(this);
            }/* else {
                removeView(child);
            }*/
        }
    }

    @Override
    public void onDispatchTouchEvent(View v, MotionEvent ev) {
        if (MotionEvent.ACTION_UP == ev.getAction()) {
            int index = (int) v.getTag(R.id.dp_TABHOST_TAG_TAB_INDEX);
            DPLogUtil.d("onClick" + index);
            if (null != onTabClickListener) {
                onTabClickListener.onTabClick(index, v);
            }
            setCurrentTab(index);
        }
    }

    public void setTabDirection(int orientation) {
        super.setOrientation(orientation);
    }

    public void setOnTabChangeListener(OnTabChangeListener listener) {
        externalTabChangeListener = listener;
    }

    public void setOnTabClickListener(OnTabClickListener listener) {
        this.onTabClickListener = listener;
    }

    public void setCurrentTab(int index) {
        DPLogUtil.d("DPTabItemContainer.setCurrentTab index=" + index + ",childCount=" + getChildCount());
        if (index >= getChildCount()) {
            return;
        }

        if (index != this.currentTabIndex) {
            tabChangeListener.beforeTabChange(currentTabIndex, getChildAt(currentTabIndex));
            if (null != currentTabWidget) {
                dispatchChecked(currentTabWidget, false);
            }
            currentTabWidget = getChildAt(index);
            dispatchChecked(currentTabWidget, true);
            this.currentTabIndex = index;
            tabChangeListener.afterTabChange(index, getChildAt(index));
        }
    }

    private void dispatchChecked(View view, boolean checked) {
        view.setSelected(checked);
//        int[] state;
//        if (checked) {
//            state = new int[]{android.R.attr.state_selected};
//        } else {
//            state = new int[]{-android.R.attr.state_selected};
//        }
//        if (null != view.getBackground()) {
//            view.getBackground().setState(state);
//        }
        if (view instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) view;
            for (int i = 0; i < vg.getChildCount(); i++) {
                dispatchChecked(vg.getChildAt(i), checked);
            }
        } else {
            if (view instanceof Checkable) {
                ((Checkable) view).setChecked(checked);
            }
        }
    }

    public int getCurrentTabIndex() {
        return this.currentTabIndex;
    }

    public View getCurrentTabWidget() {
        if (this.currentTabIndex < getChildCount()) {
            return getChildAt(this.currentTabIndex);
        }
        return null;
    }

    public void prevTab() {
        if (this.currentTabIndex == 0) {
            if (recyclable) {
                setCurrentTab(getChildCount() - 1);
            }
        } else {
            setCurrentTab(this.currentTabIndex - 1);
        }
    }

    public void nextTab() {
        if (this.currentTabIndex == getChildCount() - 1) {
            if (recyclable) {
                setCurrentTab(0);
            }
        } else {
            setCurrentTab(this.currentTabIndex + 1);
        }
    }

    public void setRecyclable(boolean recyclable) {
        this.recyclable = recyclable;
    }

    public interface OnTabChangeListener {
        void beforeTabChange(int index, View tabWidget);

        void afterTabChange(int index, View tabWidget);
    }

    public interface OnTabClickListener {
        void onTabClick(int index, View tabWidget);
    }
}
